<?php
class SigninController extends Controller{


	public function index(){
		$this->getSmarty();
		$this->smarty->assign ( 'pagesets', $this->headerDatas() );
		
		$this->smarty->display("user_center/login.tpl");
	}

    public function login(){
        $user_name = $password = $remember;
        extract ( $_POST, EXTR_IF_EXISTS );
        require_once SERVICE.DS.'admin'.DS.'UsersService.class.php';
        $userSerivce = new UsersService($this->getDB());
        $user_info = $userSerivce->checkPassword($user_name, $password );

        if (!empty($user_info->email)) {
        	$userinfo = array( 
        		'userid' => $user_info->userid,
	        	'username' => $user_info->username,
	            'email' => $user_info->email,
        		'points' => $user_info->points
        	);
            $_SESSION['user'] = $userinfo;
            echo json_encode($user_info->username);
        }else{
            echo json_encode(array(result=>1));
        }

    }
    public function userExsist($msg){
        return "<div class='alert alert-error'>$msg</div>";
    }
    public function isLogin(){
        if($_SESSION['user'] == ""){
            echo json_encode(array("result"=>false));
            return ;
        }else{
        	$points = $_SESSION['user']['points']==null?"0":$_SESSION['user']['points'];
            echo json_encode(array("result"=>true, "username"=>$_SESSION['user']['username'], "points"=>$points));
            return ;
        }
    }
    public function loginout(){
        $_SESSION['user'] = null;
        $url = WEBSITE_URL."index";
        $redirect = "<script language='javascript' type='text/javascript'>";
        $redirect .= "window.location.href='$url'";
        $redirect .= "</script>";
        echo $redirect;
    }
    public function headerDatas(){
        require_once COMMON . DS  . 'Constant.class.php';
        $js = array(WEBSITE_URL."public/assets/js/jquery.validationEngine-zh.js",WEBSITE_URL."public/assets/js/jquery.validationEngine.js");
        $css = array(WEBSITE_URL."public/assets/css/validationEngine.jquery.css");
        $pagesets = array("seo"=>Constant::$seoarray,"js"=>$js,"css"=>$css,"nav"=>Constant::getNav());

        return $pagesets;
    }
}